<?php

namespace common\models\form;

use Yii;
use yii\helpers\Json;

/**
 * This is the model class for table "{{%form_field}}".
 *
 * @property int $id
 * @property int $form_id 表单ID
 * @property int $group_id 分组ID
 * @property string $name 字段标识名称
 * @property string $label 字段名称
 * @property string $input_type 输入框类型
 * @property string $data_type 数据类型
 * @property int $required 是否必填
 * @property string|null $setting 字段配置信息
 * @property string|null $placeholder 说明信息
 * @property int $listorder 显示排序
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class FormField extends \common\db\ActiveRecord {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%form_field}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['form_id', 'group_id', 'required', 'listorder', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name', 'label'], 'required'],
            [['data_type', 'setting'], 'string'],
            [['name'], 'string', 'max' => 20],
            [['label', 'input_type'], 'string', 'max' => 50],
            [['placeholder'], 'string', 'max' => 255],
            [['form_id', 'name'], 'unique', 'targetAttribute' => ['form_id', 'name']],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'form_id' => '表单ID',
            'group_id' => '分组ID',
            'name' => '字段标识名称',
            'label' => '字段名称',
            'input_type' => '输入框类型',
            'data_type' => '数据类型',
            'required' => '是否必填',
            'setting' => '字段配置信息',
            'placeholder' => '说明信息',
            'listorder' => '显示排序',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'name',
            'label',
            'input_type',
            'data_type',
            'required' => function() {
                return boolval($this->required);
            },
            'setting' => function() {
                if ($this->setting) {
                    return Json::decode($this->setting);
                }
                return null;
            },
            'placeholder',
        ];
    }

}
